import eventEmitter from '@/utils/EventEmitter'

let cachedButtonRole = null
let obj = {}

export function useAuthButton() {
  return new Promise(resolve => {
    // 若缓存有值 直接使用
    if (cachedButtonRole !== null) {
      cachedButtonRole.forEach(buttonRole => {
        // 创建目标权限对象
        obj[buttonRole] = true
      })
      // 直接返回缓存值
      resolve(obj)
      return
    }

    // 注册监听器
    const listener = buttonRole => {
      buttonRole.forEach(buttonRole => {
        obj[buttonRole] = true
      })
      resolve(obj)
    }
    eventEmitter.on('userButtonRole', listener)
  })
}
